package com.shinaier.laundry.snlstore.util;

import android.content.Context;

import com.common.utils.StringUtil;
import com.common.utils.ToastUtil;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by 张家洛 on 2017/11/14.
 */

public class JudgePhoneUtil {

    public static String MD5(String data) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(data.getBytes());
            return bytesToHexString(bytes);
        } catch (NoSuchAlgorithmException e) {
        }
        return data;
    }

    private static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    private boolean checkPhoneNumber(Context context,String phoneNum) {
        if (StringUtil.isEmpty(phoneNum)){
            ToastUtil.shortShow(context, "请输入手机号!");
            return false;
        } else if(!"1".equals(phoneNum.trim().substring(0,1))) {
            ToastUtil.shortShow(context, "请输入正确手机号");
            return false;
        } else if(phoneNum.trim().length() != 11){
            ToastUtil.shortShow(context, "请输入正确手机号");
            return false;
        } else if(!CommonTools.checkPhoneNum(phoneNum)){
            ToastUtil.shortShow(context, "请输入正确手机号");
            return false;
        }
        return true;
    }
}
